package com.berwin.caper.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务器信息
 */
public class SrvInfo {

    // 目标机器
    private String targetHost;
    private int targetPort;
    private String targetUserName;
    private String targetPassword;

    // 堡垒机
    private String jumpHost;
    private String jumpPort;
    private String jumpUserName;
    private String jumpPassword;

    private List<Tag> tags = new ArrayList<>();

    public SrvInfo() {

    }

    public static SrvInfo parseFromJSONString(String json) {
        JSONObject object = JSONObject.parseObject(json);
        SrvInfo srvInfo = new SrvInfo();
        srvInfo.setTargetHost(object.getString("targetHost"));
        srvInfo.setTargetPort(object.getInteger("targetPort"));
        srvInfo.setTargetUserName(object.getString("targetUserName"));
        srvInfo.setTargetPassword(object.getString("targetPassword"));

        srvInfo.setJumpHost(object.getString("jumpHost"));
        srvInfo.setJumpPort(object.getString("jumpPort"));
        srvInfo.setJumpUserName(object.getString("jumpUserName"));
        srvInfo.setJumpPassword(object.getString("jumpPassword"));

        JSONArray array = object.getJSONArray("tags");
        for (int i = 0; i < array.size(); i++) {
            Tag tag = new Tag(array.getString(i));
            srvInfo.getTags().add(tag);
        }
        return srvInfo;
    }

    public String toJSONString() {
        JSONObject object = new JSONObject();
        object.put("targetHost", this.targetHost);
        object.put("targetPort", this.targetPort);
        object.put("targetUserName", this.targetUserName);
        object.put("targetPassword", this.targetPassword);
        object.put("jumpHost", this.jumpHost);
        object.put("jumpPort", this.jumpPort);
        object.put("jumpUserName", this.jumpUserName);
        object.put("jumpPassword", this.jumpPassword);

        JSONArray array = new JSONArray();
        for (Tag tag : this.tags) {
            array.add(tag.getPath());
        }
        object.put("tags", array);
        return object.toJSONString();
    }

    public String getTargetHost() {
        return targetHost;
    }

    public void setTargetHost(String targetHost) {
        this.targetHost = targetHost;
    }

    public int getTargetPort() {
        return targetPort;
    }

    public void setTargetPort(int targetPort) {
        this.targetPort = targetPort;
    }

    public String getTargetUserName() {
        return targetUserName;
    }

    public void setTargetUserName(String targetUserName) {
        this.targetUserName = targetUserName;
    }

    public String getTargetPassword() {
        return targetPassword;
    }

    public void setTargetPassword(String targetPassword) {
        this.targetPassword = targetPassword;
    }

    public String getJumpHost() {
        return jumpHost;
    }

    public void setJumpHost(String jumpHost) {
        this.jumpHost = jumpHost;
    }

    public String getJumpPort() {
        return jumpPort;
    }

    public void setJumpPort(String jumpPort) {
        this.jumpPort = jumpPort;
    }

    public String getJumpUserName() {
        return jumpUserName;
    }

    public void setJumpUserName(String jumpUserName) {
        this.jumpUserName = jumpUserName;
    }

    public String getJumpPassword() {
        return jumpPassword;
    }

    public void setJumpPassword(String jumpPassword) {
        this.jumpPassword = jumpPassword;
    }

    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }
}
